﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XLB.ExtensionMethodCore
{
    public class DotNetCoreReadSettingManager
    {
        /// <summary>
        /// 读取appsettings.json配置文件信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSettingsValue(string key)
        {
            return GetBaseSettingsValue("appsettings.json", key);
        }

        /// <summary>
        /// 读取配置文件信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetBaseSettingsValue(string path, string key)
        {
            //调用示例：DotNetCoreReadSettingManager.GetBaseSettingsValue("Configurations/App.json", "BasicConfig:EncryptionKey");

            var builder = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory) //设置基础路径
                .AddJsonFile(path, optional: false, reloadOnChange: true);  //添加json文件
            IConfigurationRoot configuration = builder.Build();
            // 读取配置值
            return configuration[key]; // 或者使用 GetSection("AppSettings").GetValue<string>("Message");
        }

    }
}
